package org.linlinjava.litemall.admin.controller.pointexchange;

import io.swagger.annotations.ApiOperation;
import org.linlinjava.litemall.admin.beans.annotation.LogAnno;
import org.linlinjava.litemall.admin.beans.dto.LumierePointExchangeDto;
import org.linlinjava.litemall.admin.controller.pointexchange.vo.LumierePointExchangePageReqVO;
import org.linlinjava.litemall.admin.service.AdminLumierePointExchangeService;
import org.linlinjava.litemall.admin.vo.pointexchange.PointExchangeStatisticReqVO;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.beans.Constants;
import org.linlinjava.litemall.db.domain.LumierePointExchange;
import org.linlinjava.litemall.db.util.BeanUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 管理卢米埃点交换控制器
 * 积分兑换
 *
 * @author yliyun
 * @date 2022/09/14
 */
@RestController
@RequestMapping("/admin/pointExchange")
@Validated
public class AdminLumierePointExchangeController {

    /**
     * 管理卢米埃点交换服务
     */
    @Autowired
    private AdminLumierePointExchangeService adminLumierePointExchangeService;

    @ApiOperation("添加")
    @PostMapping("add")
    @LogAnno
    public Object add(@RequestBody LumierePointExchangeDto dto){

        if(dto.getMaxMoney().subtract(dto.getMinMoney()).compareTo(BigDecimal.ZERO) <= 0){
            return ResponseUtil.fail(444,"金额范围最大值比最小值小");
        }

        //判断积分兑换名称是否已经存在
        List<LumierePointExchange> exchanges = adminLumierePointExchangeService.queryByName(dto.getName());
        if(exchanges.size() > 0){
            return ResponseUtil.fail(555,"积分兑换名称已经存在");
        }

        if(dto.getMinMoney() == null && dto.getMaxMoney() == null){
            return ResponseUtil.fail(666,"金额范围的最小值和最大值不能同时为空");
        }

        LumierePointExchange lumierePointExchange = BeanUtil.copy(dto,LumierePointExchange.class);
        lumierePointExchange.setStatus(Constants.POINT_EXCHANGE_STATUS_PULLED.byteValue());
        lumierePointExchange.setAddTime(LocalDateTime.now());
        lumierePointExchange.setUpdateTime(LocalDateTime.now());
        return adminLumierePointExchangeService.add(lumierePointExchange);
    }

    @ApiOperation("更新")
    @PutMapping("update")
    @LogAnno
    public Object update(@RequestBody LumierePointExchangeDto dto){
        if(dto.getId() == null){
            return ResponseUtil.fail(333,"积分兑换Id不能为空");
        }

        LumierePointExchange exchange = adminLumierePointExchangeService.findById(dto.getId());
        if(exchange == null){
            return ResponseUtil.fail(666,"积分兑换不存在");
        }
        if(dto.getMaxMoney() != null && dto.getMinMoney()==null){     //有最大值，没有最小值
            if(exchange.getMinMoney().subtract(dto.getMaxMoney()).compareTo(BigDecimal.ZERO) >=0){
                return ResponseUtil.fail(666,"修改后积分兑换的允许金额最大值小于等于最小值");
            }

        }else if(dto.getMaxMoney() == null && dto.getMinMoney() != null){    //有最小值，没有最大值
            if(dto.getMinMoney().subtract(exchange.getMaxMoney()).compareTo(BigDecimal.ZERO) >=0){
                return ResponseUtil.fail(666,"修改后积分兑换的允许金额最小值大于等于最大值");
            }
        }else if(dto.getMaxMoney() != null && dto.getMinMoney() != null){    //最大值和最小值都有
            if(dto.getMinMoney().subtract(dto.getMaxMoney()).compareTo(BigDecimal.ZERO) >=0){
                return ResponseUtil.fail(666,"修改后积分兑换的允许金额最小值大于等于最大值");
            }
        }else{
            return ResponseUtil.fail(666,"金额范围的最小值和最大值不能同时为空");
        }

        if(dto.getName() != null){
            List<LumierePointExchange> names = adminLumierePointExchangeService.queryByName(dto.getName());
            if(names.size() > 0){
                for (int i = 0; i < names.size(); i++) {
                    if(names.get(i).getId() != dto.getId()){
                        return ResponseUtil.fail(555,"积分兑换名称已经存在");
                    }
                }
            }
        }

        return adminLumierePointExchangeService.update(BeanUtil.copy(dto,LumierePointExchange.class));
    }

    /**
     * 删除
     *
     * @param pointExchangeId 点交易id
     * @return {@link Object}
     */
    @DeleteMapping("delete")
    @LogAnno
    public Object delete(@RequestParam Integer pointExchangeId){
        if(pointExchangeId == null){
            return ResponseUtil.fail(777,"积分兑换Id不能为空");
        }

        LumierePointExchange exchange = adminLumierePointExchangeService.findById(pointExchangeId);

        if(exchange == null){
            return ResponseUtil.fail(888,"积分兑换不存在");
        }

        return adminLumierePointExchangeService.delete(pointExchangeId);
    }

    /**
     * 更新状态
     * 修改上下架状态
     *
     * @param id id
     * @return {@link Object}
     */
    @PostMapping("updateStatus")
    @LogAnno
    public Object updateStatus(@RequestParam Integer id){

        if(id == null){
            return ResponseUtil.fail(777,"积分兑换Id不能为空");
        }

        LumierePointExchange ex = adminLumierePointExchangeService.findById(id);

        if(ex.getDeleted() == true){
            return ResponseUtil.fail(999,"礼物卡已经被删除，不能修改状态");
        }

        LumierePointExchange exchange = new LumierePointExchange();
        exchange.setId(id);

        if(ex.getStatus() == Constants.POINT_EXCHANGE_STATUS_PUSHED.intValue()){
            exchange.setStatus(Constants.POINT_EXCHANGE_STATUS_PULLED.byteValue());
        }else{
            exchange.setStatus(Constants.POINT_EXCHANGE_STATUS_PUSHED.byteValue());
        }
        return adminLumierePointExchangeService.update(exchange);
    }

    @ApiOperation("积分兑换列表分页")
    @GetMapping("list")
    @LogAnno
    public Object list(LumierePointExchangePageReqVO respVO){
        return adminLumierePointExchangeService.list(respVO);
    }

    @GetMapping("listAllPointExchange")
    @LogAnno
    public Object listAllPointExchange(){
        return adminLumierePointExchangeService.listAllPointExchange();
    }

    @ApiOperation("总统计")
    @PostMapping("totalStatistic")
    @LogAnno
    public Object totalStatistic(@RequestBody PointExchangeStatisticReqVO reqVO){
      return adminLumierePointExchangeService.totalStatistic(reqVO);
    }

    @ApiOperation("类型总统计")
    @PostMapping("typeTotalStatistic")
    @LogAnno
    public Object typeTotalStatistic(@RequestBody PointExchangeStatisticReqVO reqVO){
       return adminLumierePointExchangeService.typeTotalStatistic(reqVO);
    }

    /**
     * 由点交换日志
     * 查询每个积分兑换对应的明细
     *
     * @param pointExchangeId 点交易id
     * @param goodsName       商品名称
     * @param userName        用户名
     * @param exchangeTime    交换时间
     * @param shopId          商店id
     * @param page            页面
     * @param limit           限制
     * @param sort            排序
     * @param order           订单
     * @return {@link Object}
     */
    @GetMapping("logByPointExchange")
    @LogAnno
    public Object logByPointExchange( Integer pointExchangeId,String goodsName,
                                     String userName,String exchangeTime,Integer shopId,
                                     @RequestParam(defaultValue = "1") Integer page,
                                     @RequestParam(defaultValue = "10") Integer limit,
                                     @Sort @RequestParam(defaultValue = "add_time") String sort,
                                     @Order @RequestParam(defaultValue = "desc") String order){
        return adminLumierePointExchangeService.logByPointExchange(pointExchangeId,goodsName,
                userName,exchangeTime,shopId,sort,order,page,limit);
    }
}
